﻿package little.bunny;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MyBindCard extends Activity {
	private Button btn_bind;
	private Button btn_cancel;
	private EditText txtInputCard;
	private EditText txtCardPass;
	private TextView textUserCode;
	private CheckBox chk_detail;
	
	private Button backButton;
	
	private final String serviceNameSpace = "http://littlebunny.com/";
	private final String method_name = "CustomBindCard";
	private final String serviceUrl = "http://www.littlebunny.com.cn/bunny_service/bunny_service.asmx";
	private final String SOAP_ACTION = "http://littlebunny.com/CustomBindCard";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_bind_card);
		
		SharedPreferences sharedata2 = getSharedPreferences("user_data", 0);  
		String s_user = sharedata2.getString("name", null);
	    
		textUserCode = (TextView) findViewById(R.id.txtUser_Name);
        textUserCode.setText(s_user);
		txtInputCard = (EditText) findViewById(R.id.editInputCardID);
		txtCardPass = (EditText) findViewById(R.id.editInputCardPass);
		chk_detail = (CheckBox) findViewById(R.id.chk_detail);
		
		//************测试数据*******
		//txtInputCard.setText("000120");
		//txtInputPass.setText("15673393");
		//*************************
				
		btn_bind = (Button) findViewById(R.id.btn_bind_card);
		
		btn_bind.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				bind_card();
			}
		});
		
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		
		btn_cancel.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				if (chk_detail.isChecked())
				{
					Intent myIntent=new Intent();
					myIntent.setClass(MyBindCard.this, MyAddress.class);
					startActivity(myIntent);
				}
				else
				{
					Intent myIntent=new Intent();
					myIntent.setClass(MyBindCard.this, MyHome.class);
					startActivity(myIntent);
				}
			}
		});
		
		backButton = (Button) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	private void bind_card() 
	{
		if (txtInputCard.getText().toString().length() == 0) {
			ShowMsg("请输入会员卡卡号.");
			return;
		}

		if (txtCardPass.getText().toString().length() == 0) {
			ShowMsg("请输入会员卡密码.");
			return;
		}
		
		if (NetFunction.CheckNetStatus(this)==false)
		{
			ShowMsg("没有找到有效的网络.");
			return;
		}
		
		//Toast.makeText(this, "读取会员信息...", Toast.LENGTH_LONG).show();
		
		final ProgressDialog pDialog = ProgressDialog.show(this, "请稍等",
				"检测会员卡是否有效...", true);
		
		pDialog.setOnKeyListener(new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK){
					pDialog.dismiss();
				}
				return false;
			}
		});
		Thread networkThread = new Thread() {
			@Override
			public void run() {
				SoapObject request = new SoapObject(serviceNameSpace, method_name);
				request.addProperty("user_id", textUserCode.getText().toString());
				request.addProperty("card_id", txtInputCard.getText().toString());
				request.addProperty("card_pass", txtCardPass.getText().toString());

				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.dotNet = true;
				envelope.setOutputSoapObject(request);
				
				try {
					HttpTransportSE ht = new HttpTransportSE(serviceUrl);
					ht.call(SOAP_ACTION, envelope);

					final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
					final String result = response.toString();
					
					//Toast.makeText(this, "读取完毕.", Toast.LENGTH_LONG).show();
					
					pDialog.dismiss();// 隐藏进度条
					
					runOnUiThread(new Runnable() {
						public void run() {
							if (result.startsWith("99:", 0))
							{
								ShowMyDialog("绑定会员卡",result.substring(3));
							}
							else
							{
								//成功，转向
								new AlertDialog.Builder(MyBindCard.this)
								.setTitle("绑定会员卡")
								.setMessage("绑定成功")
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {
											public void onClick(DialogInterface dialog,
													int which) {
												if (chk_detail.isChecked())
												{
													Intent myIntent=new Intent();
													myIntent.setClass(MyBindCard.this, MyAddress.class);
													startActivity(myIntent);
												}
												else
												{
													Intent myIntent=new Intent();
													myIntent.setClass(MyBindCard.this, MyHome.class);
													startActivity(myIntent);
												}
											}
										}).show();
							}
						}
					});
				} catch (Exception e) {
					pDialog.dismiss();
					e.printStackTrace();
					//Toast.makeText(this, "网络错误,请检查能否访问网络", Toast.LENGTH_LONG).show();
					//TextView txt1;
					//txt1 = (TextView) findViewById(R.id.textShowJiFen);
					//txt1.setText("网络错误,请检查能否访问网络");
				}
			}
		};

		networkThread.start();
	}
	
	private void ShowMsg(String msg)
	{
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
	
	private void ShowMyDialog(String title, String info)
	{
		new AlertDialog.Builder(MyBindCard.this)
		.setTitle(title)
		.setMessage(info)
		.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						//MyLogin.this.finish();
					}
				}).show();
	}
}
